package nahuel.games.carracer;

import android.graphics.Rect;
import android.os.Bundle;

public class Vida {

	private Rect barra;
	private Rect total;
	private int porcentaje;
	private int ancho;
	
	private static final int COEF_BARRA = 5;
	private static final int VIDA_CHOQUE = 5;
	static final String LEFT_X = "leftX";
	static final String BOTTOM_Y = "bottomY";
	
	public Vida(int ancho){
		barra = new Rect();
		total = new Rect();
		this.ancho = ancho;
		porcentaje = 100;
	}
	
	public void bajarVida(){
		if(tieneVida())
			porcentaje -= VIDA_CHOQUE;
	}
	
	public void setX(int x){
		total.left = barra.left = x;
		total.right = x + ancho;
	}
	
	public void setY(int y){
		total.bottom = barra.bottom = y;
		total.top = barra.top = y - (ancho / COEF_BARRA);
	}
	
	public Rect getTotal(){
		return total;
	}
	
	public Rect getBarra(){
		barra.right = barra.left + (ancho * porcentaje / 100);
		return barra;
	}
	
	public boolean tieneVida(){
		return porcentaje > 0;
	}

	public Bundle getBundle() {
		Bundle bund = new Bundle();
		bund.putInt(LEFT_X, total.left);
		bund.putInt(BOTTOM_Y, total.bottom);
		return bund;
	}

	public void setBundle(Bundle bundle) {
		setX(bundle.getInt(LEFT_X));
		setY(bundle.getInt(BOTTOM_Y));
	}
}
